﻿using System;
using Microsoft.Xna.Framework;

namespace BattleFury.Lib
{
    public class Movement
    {
        private float _direction = 0;
        private float _velocity = 0;
        private float _XVelocity = 0;
        private float _YVelocity = 0;

        public Movement(Vector2 position, Vector2 newDest)
        {
            ///http://stackoverflow.com/questions/6064630/get-angle-from-2-positions
            //Center points so Position is 0,0
            Destination = newDest;
            if (position.X != newDest.X || position.Y != newDest.Y)
            {
                Direction = (float)Math.Atan2(newDest.Y - position.Y, newDest.X - position.X);
                Velocity = 1;
            }
            else
            {
                Direction = 0;
                Velocity = 0;
            }
        }

        public Movement() { }

        public Movement(Vector2 Destination, float Velocity)
        {
            // TODO: Complete member initialization
            this.Destination = Destination;
            this.Velocity = Velocity;
        }

        public Vector2 Destination { get; set; }

        public float Direction
        {
            get { return _direction; }
            set
            {
                _direction = value;
                GetXYVelocities();
            }
        }

        public float Velocity
        {
            get { return _velocity; }
            set
            {
                _velocity = value;
                GetXYVelocities();
            }
        }

        public Vector2 UpdatePosition(double timeIncrement, Vector2 currentPosition)
        {
            Vector2 newPosition = new Vector2(
                 currentPosition.X + (float)(timeIncrement * _XVelocity * 70),
                 currentPosition.Y + (float)(timeIncrement * _YVelocity * 70));

            if ((_XVelocity < 0 && newPosition.X < Destination.X) || (_XVelocity > 0 && newPosition.X > Destination.X))
                newPosition.X = Destination.X;
            if ((_YVelocity < 0 && newPosition.Y < Destination.Y) || (_YVelocity > 0 && newPosition.Y > Destination.Y))
                newPosition.Y = Destination.Y;

            return newPosition;
        }

        private void GetXYVelocities()
        {
            //because you are stupid ryan and you will forget where you got this from.
            //http://stackoverflow.com/questions/2093405/finding-the-coordinates-on-the-edge-of-a-circle
            if (Velocity == 0)
            {
                _XVelocity = 0;
                _YVelocity = 0;
            }
            else
            {
                _XVelocity = (float)Math.Cos(_direction);
                _YVelocity = (float)Math.Sin(_direction);
            }
        }

        public bool IsMoving
        {
            get
            {
                return !(_XVelocity == 0 && _YVelocity == 0);
            }
        }
    }
}